require Goliath::Application.root_path('app/mailers/registration_mailer.rb')

resource :registration do
  post '/register' do
    user = User.create(params[:user])
    hash = {ok: user.errors.empty?}
    return hash.merge! errors: user.errors if user.errors.any?
    RegistrationMailer.confirmation(user.id).deliver
    hash
  end

  get '/confirm' do
    user = User.where(id: params[:id]).first
    return {ok: false, errors: {id: ['not exist']}} unless user
    return {ok: false, errors: {activation_token: ['is wrong']}} unless user.activation_token == params[:token]

    user.activated_at = Time.now
    user.save

    {ok: user.errors.empty?}.merge! user.as_json
  end
end
